package net.wanho.ums.servlet;

import net.wanho.ums.entity.User;
import net.wanho.ums.service.UserService;
import net.wanho.ums.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author：汤小洋
 * Date：2023-06-29 17:23
 * Description：<描述>
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        if ("/login.user".equals(path)){
            login(req,resp);
        }else if ("/register.user".equals(path)){
            register(req,resp);
        }
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        try {
            User user = userService.login(username, password);
            req.getSession().setAttribute("user",user);
            // 转发到main.jsp
            req.getRequestDispatcher("WEB-INF/main.jsp").forward(req,resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private void register(HttpServletRequest req, HttpServletResponse resp) {

    }
}
